package com.taotao.product.service.impl;

import com.taotao.feign.SearchFeignService;
import com.taotao.product.domain.PmsSpuInfo;
import com.taotao.product.vo.SaleAttrVo;
import com.taotao.search.response.ProductSkuResponse;
import com.taotao.product.mapper.PmsSpuInfoMapper;
import com.taotao.product.service.PmsSpuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @className: PmsSpuInfoServiceImpl
 * @author: ZhangYuQi
 * @date: 2024/12/19 上午11:00
 * @Version: 1.0
 * @description:
 */

@Service
public class PmsSpuInfoServiceImpl implements PmsSpuInfoService {

    @Autowired
    private PmsSpuInfoMapper pmsSpuInfoMapper;

    @Autowired
    private SearchFeignService searchFeignService;

    /**
    * @Author ZhangYuQi
    * @Description 商品上架
    * @Date 上午11:03 2024/12/19
    * @Param [spuId]
    * @return
    */
    @Override
    public void up(Integer spuId) {
        //根据spuId 查询商品详情信息
        PmsSpuInfo spuInfo = pmsSpuInfoMapper.selectSpuById(spuId);
        if(spuInfo==null){
            throw new RuntimeException("商品不存在");
        }
        //判断商品是否已经上架
        if(spuInfo.getPublishStatus()==1){
            throw new RuntimeException("商品已经上架,请勿重复操作");
        }
        //查询商品详情信息
        List<ProductSkuResponse> productSkuResponseList = pmsSpuInfoMapper.selectAllSkuList(spuId);

        //远程调用 批量添加到es
        searchFeignService.saveProduct(productSkuResponseList);

        //更新商品状态
        spuInfo.setPublishStatus(1);
        pmsSpuInfoMapper.updatePmsSpuById(spuInfo);

    }


}
